<?php
/**
 * Description: 支付宝移动通知url
 * User: devforma
 * Date: 15/11/17
 * Time: 10:34
 */

require PUBLIC_DIR . '/payment/includes/Alipay.class.php';
require PUBLIC_DIR . '/payment/includes/OrderProcess.class.php';

class AlipayMobileNotifyUrl implements iController {
    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        $payment = new Alipay();
        if (!$payment->checkSign('rsa', $queryArgs)) return 'fail';

        if (('TRADE_SUCCESS' == $queryArgs['trade_status'] || 'TRADE_FINISHED' == $queryArgs['trade_status'])) {

            $alipayUser = empty($queryArgs['buyer_email']) ? '' : $queryArgs['buyer_email'];
            $orderProcess = new OrderProcess($queryArgs['out_trade_no'], $queryArgs['trade_no'], $alipayUser, 'alipay');

            if ($orderProcess->isExists() && !$orderProcess->isPurchased()) {
                $orderProcess::clearCache($queryArgs['out_trade_no']);

                if (!$orderProcess->finish()) return 'fail';

                return 'success';
            }
        }

        return 'fail';
    }
}
